﻿#pragma execution_character_set("utf-8")

#include "sophomore_widget.h"
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QtDebug>
#include <QLabel>
#include <QSpinBox>
#include <QLineEdit>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QFileDialog>
#include <QJsonDocument>
#include <QStandardPaths>
#include "tools/tools.h"
#include "ui/line_checkbox.h"
#include "ui/config_dialog.h"

#include "secondmathimplabc1.h"
#include "secondmathimplabc2.h"
#include "secondmathimplabc3.h"
#include "secondmathimplabc4.h"
#include "secondmathimplabc5.h"
#include "secondmathimplabc6.h"
#include "secondmathimplabc7.h"
#include "secondmathimplabc8.h"
#include "secondmathimplabc9.h"

SophomoreWidget::SophomoreWidget(QWidget *parent)
    : QWidget(parent)
    , m_lineCheckBox1(new LineCheckBox("题型:a*b+c",new SecondMathImplABC1(this) ,this))
    , m_lineCheckBox2(new LineCheckBox("题型:a*b-c",new SecondMathImplABC2(this),this))
    , m_lineCheckBox3(new LineCheckBox("题型:a/b+c",new SecondMathImplABC3(this),this))
    , m_lineCheckBox4(new LineCheckBox("题型:a/b-c",new SecondMathImplABC4(this),this))
    , m_lineCheckBox5(new LineCheckBox("题型:a+-b+-c",new SecondMathImplABC5(this),this))
    , m_lineCheckBox6(new LineCheckBox("题型:a*b",new SecondMathImplABC6(this),this))
    , m_lineCheckBox7(new LineCheckBox("题型:a/b",new SecondMathImplABC7(this),this))
    , m_lineCheckBox8(new LineCheckBox("题型:a+b",new SecondMathImplABC8(this),this))
    , m_lineCheckBox9(new LineCheckBox("题型:a-b",new SecondMathImplABC9(this),this))
    , m_layoutNum(new QHBoxLayout)
    , m_layoutMain(new QVBoxLayout)
    , m_btn(new QPushButton("生成题目",this))
    , m_btnSavevFile(new QPushButton(tr("选择保存文件路径")))
    , m_edtSaveFileName(new QLineEdit())
    , m_layoutSaveFile(new QHBoxLayout())
    , m_labNum(new QLabel("题目数量："))
    , m_SpinBoxNum(new QSpinBox)
{
    initUI();
    initConnect();
    initLineCheckStatus();
}

void SophomoreWidget::slotClickBtn()
{
    if(m_edtSaveFileName->text().isEmpty()){
        QMessageBox::information(this,"提示","为选中文件");
        return ;
    }
    functionPointers.clear();
    for(auto item : m_listCheckBoxs){
        if(item->isChecked()){
            functionPointers.push_back(item);
        }
    }

    if(functionPointers.size() == 0){
        QMessageBox::information(this,"提示","为选题行");
        return;
    }
    QFile file(m_edtSaveFileName->text());
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QMessageBox::information(this,"提示","文件写失败");
        file.close();
        return;
    }
    QTextStream in(&file);
    in.setCodec("utf-8");
    for (int i = 0 ; i< m_SpinBoxNum->value(); i++) {
        int max = static_cast<int>(functionPointers.size()-1);
        int r =   m_tools.generateRandomNumber(0, max);
        QString result = functionPointers.at(r)->fun();
         in << result;
         in <<  "\n";
    }
    file.close();

    saveConfigFile();
    QMessageBox::information(this,"提示","任务完成");
}

void SophomoreWidget::slotClickSaveBtn()
{
    QString file_path = QFileDialog::getSaveFileName(this,tr("save as "),"",tr("*.txt"));
    m_edtSaveFileName->setText(file_path);
}

void SophomoreWidget::slotClickConfigBtn1()
{
    qWarning()<<"as";
    ConfigDialog config("SecondGrade","1.json");
    config.exec();
}

void SophomoreWidget::initUI()
{
    m_SpinBoxNum->setMaximum(1000);
    m_SpinBoxNum->setValue(100);

    QString strDesktopLocation = Tools::instance()->getHomeDir();
    m_edtSaveFileName->setText(strDesktopLocation);
    m_layoutSaveFile->addWidget(m_btnSavevFile);
    m_layoutSaveFile->addWidget(m_edtSaveFileName);

    m_layoutNum->addWidget(m_labNum);
    m_layoutNum->addWidget(m_SpinBoxNum);
    m_layoutMain->addLayout(m_layoutSaveFile);
    m_layoutMain->addLayout(m_layoutNum);
    m_layoutMain->addWidget(m_btn);

    m_layoutMain->addWidget(m_lineCheckBox8);
    m_layoutMain->addWidget(m_lineCheckBox9);
    m_layoutMain->addWidget(m_lineCheckBox6);
    m_layoutMain->addWidget(m_lineCheckBox7);
    m_layoutMain->addWidget(m_lineCheckBox1);
    m_layoutMain->addWidget(m_lineCheckBox2);
    m_layoutMain->addWidget(m_lineCheckBox3);
    m_layoutMain->addWidget(m_lineCheckBox4);
    m_layoutMain->addWidget(m_lineCheckBox5);

    m_layoutMain->addStretch();
    setLayout(m_layoutMain);
}

void SophomoreWidget::initConnect()
{
    connect(m_btn,&QPushButton::clicked,this,&SophomoreWidget::slotClickBtn);
    connect(m_btnSavevFile,&QPushButton::clicked,this,&SophomoreWidget::slotClickSaveBtn);
    connect(m_lineCheckBox6,&LineCheckBox::sigClickConfigBtn,this,&SophomoreWidget::slotClickConfigBtn1);
}

void SophomoreWidget::initLineCheckStatus()
{
    m_listCheckBoxs.push_back(m_lineCheckBox8);
    m_listCheckBoxs.push_back(m_lineCheckBox9);
    m_listCheckBoxs.push_back(m_lineCheckBox6);
    m_listCheckBoxs.push_back(m_lineCheckBox7);
    m_listCheckBoxs.push_back(m_lineCheckBox1);
    m_listCheckBoxs.push_back(m_lineCheckBox2);
    m_listCheckBoxs.push_back(m_lineCheckBox3);
    m_listCheckBoxs.push_back(m_lineCheckBox4);
    m_listCheckBoxs.push_back(m_lineCheckBox5);

    QString strConfigPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    strConfigPath += "/SecondGrade";
    QString strConfigFile  = strConfigPath +"/info.json";
    QFile file(strConfigFile);
    if(file.open(QIODevice::ReadOnly))
    {
        QJsonDocument jdoc = QJsonDocument::fromJson(file.readAll());
        QJsonObject jobject = jdoc.object();
        for(int i = 0 ; i < m_listCheckBoxs.size() ;i++){
            m_listCheckBoxs.at(i)->setChecked(jobject.value("fun"+QString::number(i)).toBool());
        }
    }
}

void SophomoreWidget::saveConfigFile()
{
    QString strConfigPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    strConfigPath += "/SecondGrade";
    QDir dir(strConfigPath);
    if(!dir.exists()){
        dir.mkpath(strConfigPath);
    }
    QJsonObject jObject;
    QJsonDocument jDoc;
    for(int i = 0 ; i < m_listCheckBoxs.size() ;i++){
       jObject.insert("fun"+QString::number(i),m_listCheckBoxs.at(i)->isChecked());
    }
    jDoc.setObject(jObject);
    QString strConfigFile  = strConfigPath +"/info.json";
    QFile file(strConfigFile);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream out(&file);
        out << jDoc.toJson();
        file.close();
    }

}
